<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/5/7
 * Time: 14:25
 */

namespace zhtfrontend\controllers;

use common\models\datas\Site;
use zhtfrontend\components\bases\BaseController;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\Mechanic;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\web\BadRequestHttpException;
use common\components\helpers\Tree;
use yii\widgets\ActiveForm;

class MechanicshowController extends BaseController
{
    public function actionIndex(){
        $site=Site::findOne(['id'=>Fun::getSiteId()]);
        if($site==null){
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        if(\Yii::$app->request->isAjax){
            \Yii::$app->response->format = Response::FORMAT_JSON;
            $ajax_object = new AjaxObject();
            $mechanic = Mechanic::find()->where(['site_id'=>Fun::getSiteId(),'is_delete'=>0]);
            $mids = Fun::getMechanicIds();
            if(!empty($mids)){
                $mechanic->andWhere(['id'=>$mids]);
            }
            $mechanic = $mechanic->indexBy('id')->orderBy(['id'=>SORT_ASC])->all();
            if(count($mechanic) == 1){
                $tree = Fun::asArray($mechanic, ['zhtfrontend_operate']);
            }else {
                $tree = (new Tree(Fun::asArray($mechanic, ['zhtfrontend_operate'])))->get_jquery_tree_array();
            }
            $ajax_object->data = $tree;
            return $ajax_object;
        }
        return $this->render('index',['mode'=>$site->mode]);
    }

    public function actionUpdate()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '错误请求'));
        }
        $model = Mechanic::findOne(\Yii::$app->request->get('id'));
        $model->scenario = 'update_show';
        if ($model == null) {
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        $site=Site::findOne(['id'=>Fun::getSiteId()]);
        if($site==null){
            throw new NotFoundHttpException(\Yii::t('zhtfrontend', '信息不存在'));
        }
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $model->user_id = \Yii::$app->user->id;
            $postdata = \Yii::$app->request->post();
            if(!empty($postdata['Mechanic']['imageList'])){
                $imagelist = $postdata['Mechanic']['imageList'];
                $imagelist_arr = [];
                if(!empty($imagelist)){
                    $imagelist_arr = explode('#|#',$imagelist);
                }
                if(!empty($postdata['Mechanic']['defaulturl'])&&in_array($postdata['Mechanic']['defaulturl'],$imagelist_arr)){
                    $model->picture = $postdata['Mechanic']['defaulturl'];
                }else{
                    if(count($imagelist_arr)>0){
                        $model->picture = $imagelist_arr[0];//上传的列表第一个作为展示的默认图
                    }else{
                        $model->picture = '';
                    }
                }
            }else{
                $model->picture = '';
            }
            if ($model->save(false)) {
                return $this->redirectMessage(\Yii::t('zhtfrontend', '修改成功'), Fun::url(['mechanicshow/index']));
            }
        }
        return $this->render('create', ['mode'=>$site->mode,'model'=>$model]);
    }


}
